package com.axxc.jpa.task.rabbitmsg.provider;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.axxc.jpa.out.model.Student;

/**
 * @ClassName: RabbitMqEntityMsgProviderTask
 * @Description: 实体消息生产者
 * @author xxc
 * @date 2018年8月3日 下午4:57:41
 */
//@Component
public class RabbitMqEntityMsgProviderTask {

	private static final Random radnom = new Random();

	private static final int PROVIDER_COUNT = 100;// 每分钟随机产生条数

	@Autowired
	private AmqpTemplate rabbitTemplate;

	@Async
	@Scheduled(cron = "0 0/1 * * * *") // cron表达式 每分钟执行一次
	public void reportCurrentTime() {
		Student e;
		int nextInt = radnom.nextInt(PROVIDER_COUNT);
		System.err.println("随机产生的条数:" + nextInt);
		for (int i = 0; i < nextInt; i++) {
			e = new Student();
			e.setName("熊骑士" + i);
			e.setAge(i);
			e.setSex((byte) (i % 2 == 0 ? 1 : 0));
			e.setCreateTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			this.rabbitTemplate.convertAndSend("user", e);
		}

	}
}
